/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import static com.lineage.server.model.skill.L1SkillId.SHAPE_CHANGE;

import com.lineage.server.PacketCreate;
import com.lineage.server.datatables.PolyTable;
import com.lineage.server.model.L1PolyMorph;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.utils.Random;

/**
 * Class <code>PolyScrollWelfare</code> 福利变形药水 - 49308.
 * 
 * @author jrwz
 * @version 2012-6-19下午04:33:40
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class PolyScrollWelfare extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new PolyScrollWelfare();
    }

    /** 福利变形药水 - 49308. */
    private PolyScrollWelfare() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        // 取回字符串
        final String text = pc.getText();
        // 清空字符串
        pc.setText(null);
        // 变身时间随机
        int time = 0;
        time = Random.nextInt(2401, 4800);
        final L1PolyMorph poly = PolyTable.getInstance().getTemplate(text);

        if ((poly != null) || text.equals("")) {
            if (text.equals("")) {
                pc.removeSkillEffect(SHAPE_CHANGE);
            } else if ((poly.getMinLevel() <= pc.getLevel()) || pc.isGm()) {
                L1PolyMorph.doPoly(pc, poly.getPolyId(), time,
                        L1PolyMorph.MORPH_BY_ITEMMAGIC);
            }
            pc.getInventory().removeItem(useItem, 1);
        } else {
            PacketCreate.get().sendSinglePackets(pc, "无法变成你指定的怪物");
        }
    }
}
